package com.jy.bo.common.conf.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.utils.HttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: binary
 * Date: 2020/8/20
 * Time: 下午3:42
 */
public class DingTalkPushUtils {

    private static final String DING_URL = "https://oapi.dingtalk.com/robot/send?access_token=eebdd6e5418bc552c0d0d00dd38545d7982d022126fdf118aeacd3373b3b2562";

    /**
     * 推送文字消息到所有人
     *
     * @param content 文字内容
     * @return
     */
    public static boolean pushTextMessageAtAll(String content) {
        try {
            String paramBody = getPostBodyObject(content, true, null).toJSONString();
            String response = HttpUtil.postPageBody(DING_URL, getPostHeaderMap(), paramBody);
            if (JSON.parseObject(response).getInteger("errcode") != 0) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 推送文字消息到所有人
     *
     * @param webhook 机器人hook链接
     * @param content 文字内容
     * @return
     */
    public static boolean pushTextMessageAtAll(String webhook, String content) {
        try {
            String paramBody = getPostBodyObject(content, true, null).toJSONString();
            String response = HttpUtil.postPageBody(webhook, getPostHeaderMap(), paramBody);
            if (JSON.parseObject(response).getInteger("errcode") != 0) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 推送文字消息到部分人
     *
     * @param content 文字内容
     * @param mobiles 手机号码列表
     * @return
     */
    public static boolean pushTextMessageAtPart(String content, List<String> mobiles) {
        try {
            JSONObject postBodyObject = getPostBodyObject(content, false, mobiles);
            String paramBody = postBodyObject.toJSONString();
            String response = HttpUtil.postPageBody(DING_URL, getPostHeaderMap(), paramBody);
            if (JSON.parseObject(response).getInteger("errcode") != 0) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 推送文字消息到部分人
     *
     * @param webhook 机器人hook链接
     * @param content 文字内容
     * @param mobiles 手机号码列表
     * @return
     */
    public static boolean pushTextMessageAtPart(String webhook, String content, List<String> mobiles) {
        try {
            JSONObject postBodyObject = getPostBodyObject(content, false, mobiles);
            String paramBody = postBodyObject.toJSONString();
            String response = HttpUtil.postPageBody(webhook, getPostHeaderMap(), paramBody);
            if (JSON.parseObject(response).getInteger("errcode") != 0) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    private static JSONObject getPostBodyObject(String content, boolean atAll, List<String> mobiles) {
        JSONObject textObject = new JSONObject();
        textObject.put("content", content);
        JSONObject atObject = new JSONObject();
        if (atAll) {
            atObject.put("isAtAll", atAll);
        }
        if (mobiles != null && mobiles.size() > 0) {
            atObject.put("atMobiles", mobiles);
        }
        JSONObject postBodyObject = new JSONObject();
        postBodyObject.put("msgtype", "text");
        postBodyObject.put("text", textObject);
        postBodyObject.put("at", atObject);
        return postBodyObject;
    }

    private static Map getPostHeaderMap() {
        Map<String, String> postHeaderObject = new HashMap<>(1);
        postHeaderObject.put("Content-Type", "application/json");
        return postHeaderObject;
    }
}
